package net.seninp.fdata.data;

import java.util.ArrayList;
import java.util.Collections;

public class Cell {

  private Double minLatd;
  private Double maxLatd;
  private Double minLond;
  private Double maxLond;
  private Years years;

  public Cell(Double minLatd, Double maxLatd, Double minLond, Double maxLond) {
    this.minLatd = minLatd;
    this.maxLatd = maxLatd;
    this.minLond = minLond;
    this.maxLond = maxLond;

    this.years = new Years();
  }

  public boolean add(Measurement m) {
    double lat = m.getLatd();
    double lon = m.getLond();
    if ((this.minLatd <= lat && lat < this.maxLatd) && (this.minLond <= lon && lon < this.maxLond)) {
      this.years.add(m);
      return true;
    }
    return false;
  }

  public Integer measurementsCount() {
    return countMeasurements();
  }

  public boolean hasCompleteYears() {
    return fullYearsCount() > 0;
  }

  public Integer fullYearsCount() {
    int fullCount = 0;
    for (Year y : this.years) {
      if (y.isComplete()) {
        fullCount++;
      }
    }
    return fullCount;
  }

  public Integer partialYearsCount() {
    int partCount = 0;
    for (Year y : this.years) {
      if (!y.isComplete()) {
        partCount++;
      }
    }
    return partCount;
  }

  private Integer countMeasurements() {
    int count = 0;
    for (Year y : this.years) {
      count = count + y.getMeasurementsCount();
    }
    return count;
  }

  public Double getMaxLond() {
    return this.maxLond;
  }

  public Double getMinLond() {
    return this.minLond;
  }

  public Double getMinLatd() {
    return this.minLatd;
  }

  public Double getMaxLatd() {
    return this.maxLatd;
  }

  /**
   * Consolidated set full years.
   *
   * @return
   */
  public ArrayList<Integer> getFullYears() {
    ArrayList<Integer> res = new ArrayList<Integer>();
    for (Year y : this.years) {
      if (y.isComplete()) {
        res.add(y.getYear());
      }
    }
    Collections.sort(res);
    return res;
  }

  /**
   * Particular fishery full years.
   *
   * @return
   */
  public ArrayList<Integer> getFullYears(String fishery) {
    ArrayList<Integer> res = new ArrayList<Integer>();
    for (Year y : this.years) {
      if (y.isComplete(fishery)) {
        res.add(y.getYear());
      }
    }
    Collections.sort(res);
    return res;
  }

  public Year getYear(Integer y) {
    return this.years.get(y);
  }

  public double[] getEffort(String fishery, Integer y) {
    double[] res = new double[12];
    for (int m = 1; m < 13; m++) {
      res[m - 1] = this.years.get(y).getMonth(m).getAverageEffortForFishery(fishery);
    }
    return res;
  }

  public double[] getCatch(String fishery, Integer y) {
    double[] res = new double[12];
    for (int m = 1; m < 13; m++) {
      res[m - 1] = this.years.get(y).getMonth(m).getAverageCatchForFishery(fishery);
    }
    return res;
  }

  public double[] getEffectiveness(String fishery, Integer y) {
    double[] res = new double[12];
    for (int m = 1; m < 13; m++) {
      res[m - 1] = this.years.get(y).getMonth(m).getAverageEffectivenessForFishery(fishery);
    }
    return res;
  }
}
